package com.ruoyi.zxjd.conference.service.impl;

import java.util.*;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.SmsUtil;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.zxjd.common.CommonConstants;
import com.ruoyi.zxjd.common.domain.CommonUserVO;
import com.ruoyi.zxjd.common.mapper.CommonMapper;
import com.ruoyi.zxjd.conference.domain.ConferenceParticipant;
import com.ruoyi.zxjd.conference.service.IConferenceParticipantService;
import com.ruoyi.zxjd.conference.service.IConferenceProgressService;
import com.ruoyi.zxjd.work.domain.WorkHandle;
import com.ruoyi.zxjd.work.domain.WorkProgress;
import com.ruoyi.zxjd.work.service.IWorkHandleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.zxjd.conference.domain.ConferenceProgress;
import com.ruoyi.zxjd.conference.mapper.ConferenceArrangeMapper;
import com.ruoyi.zxjd.conference.domain.ConferenceArrange;
import com.ruoyi.zxjd.conference.service.IConferenceArrangeService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;
import javax.validation.constraints.Size;

/**
 * 会议安排Service业务层处理
 * 
 * @author lcc
 * @date 2021-02-07
 */
@Service
public class ConferenceArrangeServiceImpl implements IConferenceArrangeService 
{
    @Resource
    private ConferenceArrangeMapper conferenceArrangeMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Resource
    private CommonMapper commonMapper;

    @Autowired
    private IWorkHandleService workHandleService;

    @Autowired
    private IConferenceProgressService conferenceProgressService;

    @Autowired
    private IConferenceParticipantService conferenceParticipantService;



    /**
     * 查询会议安排
     * 
     * @param arrangeId 会议安排ID
     * @return 会议安排
     */
    @Override
    public ConferenceArrange selectConferenceArrangeById(Long arrangeId)
    {
        return conferenceArrangeMapper.selectConferenceArrangeById(arrangeId);
    }

    /**
     * 查询会议安排列表
     * 
     * @param conferenceArrange 会议安排
     * @return 会议安排
     */
    @Override
    public List<ConferenceArrange> selectConferenceArrangeList(ConferenceArrange conferenceArrange)
    {
        return conferenceArrangeMapper.selectConferenceArrangeList(conferenceArrange);
    }

    /**
     * 新增会议安排
     * 
     * @param conferenceArrange 会议安排
     * @return 结果
     */
    @Transactional
    @Override
    public int insertConferenceArrange(ConferenceArrange conferenceArrange)
    {
        SysUser sysUser = sysUserService.selectUserByLoginName(ShiroUtils.getLoginName());
        conferenceArrange.setCreateBy(sysUser.getUserName());
        conferenceArrange.setCreateTime(DateUtils.getNowDate());
        conferenceArrange.setDeptId(sysUser.getDeptId());
        conferenceArrange.setDeptName(sysUser.getDept().getDeptName());
        conferenceArrange.setUserId(sysUser.getUserId());
        conferenceArrange.setUserName(sysUser.getUserName());
        int rows = conferenceArrangeMapper.insertConferenceArrange(conferenceArrange);
        //insertConferenceProgress(conferenceArrange);
        return rows;
    }

    /**
     * 修改会议安排
     * 
     * @param conferenceArrange 会议安排
     * @return 结果
     */
    @Transactional
    @Override
    public int updateConferenceArrange(ConferenceArrange conferenceArrange)
    {
        conferenceArrangeMapper.deleteConferenceProgressByArrangeId(conferenceArrange.getArrangeId());
        conferenceParticipantService.deleteConferenceParticipantByArrangeId(conferenceArrange.getArrangeId());
        SysUser sysUser = sysUserService.selectUserByLoginName(ShiroUtils.getLoginName());
        conferenceArrange.setUpdateBy(sysUser.getUserName());
        conferenceArrange.setUpdateTime(DateUtils.getNowDate());
        //insertConferenceProgress(conferenceArrange);
        return conferenceArrangeMapper.updateConferenceArrange(conferenceArrange);
    }

    /**
     * 删除会议安排对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteConferenceArrangeByIds(String ids)
    {
        conferenceArrangeMapper.deleteConferenceParticipantByArrangeIds(Convert.toStrArray(ids));
        conferenceArrangeMapper.deleteConferenceProgressByArrangeIds(Convert.toStrArray(ids));
        return conferenceArrangeMapper.deleteConferenceArrangeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会议安排信息
     * 
     * @param arrangeId 会议安排ID
     * @return 结果
     */
    @Override
    public int deleteConferenceArrangeById(Long arrangeId)
    {
        conferenceArrangeMapper.deleteConferenceProgressByArrangeId(arrangeId);
        return conferenceArrangeMapper.deleteConferenceArrangeById(arrangeId);
    }

    /**
     * 发布会议安排
     * @param arrangeId
     * @param draft
     * @return
     */
    @Override
    @Transactional
    public int updateStatusByArrangeId(Long arrangeId, String draft) {
        ConferenceArrange conferenceArrange = conferenceArrangeMapper.selectConferenceArrangeById(arrangeId);
        conferenceArrange.setStatus(draft);
        if ("1".equals(draft)){
            insertConferenceProgress(conferenceArrange);
        }
        if ("0".equals(draft)){
            conferenceProgressService.deleteConferenceProgressByArrangeId(arrangeId);
            conferenceParticipantService.deleteConferenceParticipantByArrangeId(arrangeId);
        }
        return conferenceArrangeMapper.updateStatusByArrangeId(arrangeId,draft);
    }


    /**
     * 新增填报进度信息
     * 
     * @param conferenceArrange 会议安排对象
     */
    public void insertConferenceProgress(ConferenceArrange conferenceArrange)
    {

        String[] ids = new String[0];
        String releaseScope = conferenceArrange.getReleaseScope();
        String[] userNames = releaseScope.split(" ");
        for (int i=0;i<userNames.length;i++){
            SysUser sysUser = sysUserService.selectUserByName(userNames[i]);
            ids = Arrays.copyOf(ids, ids.length + 1); // 数组增加一个长度
            ids[ids.length - 1] = sysUser.getUserId()+""; // 在增加的位置赋值需要添加的数据
        }
        conferenceArrange.setUserIds(ids);
        String[] userIds = conferenceArrange.getUserIds();
        List<CommonUserVO> list = null;

        if(CommonConstants.AllUSER.equals(releaseScope)){
            list = commonMapper.listActiveUser(null,null);
        }else{
            list = commonMapper.listActiveUserByIds(userIds);
        }
        List<ConferenceProgress> conferenceProgressList = new ArrayList<>();
        if(StringUtils.isNotNull(list)) {
            for (int i = 0; i < list.size(); i++) {
                CommonUserVO userVO = list.get(i);
                ConferenceProgress conferenceProgress = new ConferenceProgress();
                conferenceProgress.setArrangeId(conferenceArrange.getArrangeId());
                conferenceProgress.setArrangeName(conferenceArrange.getArrangeName());
                conferenceProgress.setReceiveUserId(userVO.getUserId());
                conferenceProgress.setReceiveDeptId(userVO.getDeptId());
                conferenceProgress.setReceiveUser(userVO.getUserName());
                conferenceProgress.setReceiveDept(userVO.getDeptName());
                conferenceProgress.setReceiveTime(DateUtils.getNowDate());
                conferenceProgress.setLastFullTime(conferenceArrange.getLastFullTime());
                conferenceProgress.setStatus("0");
                conferenceProgress.setCreateBy(sysUserService.selectUserByLoginName(ShiroUtils.getLoginName()).getUserName());
                conferenceProgress.setCreateTime(DateUtils.getNowDate());
                //conferenceProgress.setUpdateBy(ShiroUtils.getLoginName());
                //conferenceProgress.setUpdateTime(DateUtils.getNowDate());
                conferenceProgress.setRemark(conferenceArrange.getRemark());
                conferenceProgress.setUserId(conferenceArrange.getUserId());
                conferenceProgress.setUserName(conferenceArrange.getUserName());
                conferenceProgress.setDeptId(conferenceArrange.getDeptId());
                conferenceProgress.setDeptName(conferenceArrange.getDeptName());
                conferenceProgressList.add(conferenceProgress);
                //每50条进行新增插入
                if (conferenceProgressList.size() % 50 == 0 || i == list.size() - 1) {
                    conferenceArrangeMapper.batchConferenceProgress(conferenceProgressList);
                    conferenceProgressList.clear();
                }
            }

            if (StringUtils.equals(CommonConstants.PUBLISHED, conferenceArrange.getStatus())) {
                //短信提示
                if (StringUtils.equals(CommonConstants.TIPS, conferenceArrange.getSmsTips())) {
                    AsyncManager.me().execute(createSmsTips(conferenceArrange.getArrangeName(), list,conferenceArrange));
                }
                //平台提示
                if (StringUtils.equals(CommonConstants.TIPS, conferenceArrange.getWebsiteTips())) {
                    AsyncManager.me().execute(createWebsiteTips(list, 1));
                }
            }
        }
    }

    private TimerTask createSmsTips(String arrangeTitle, List<CommonUserVO> list,ConferenceArrange conferenceArrange) {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                for (CommonUserVO commonUserVO : list) {
                    String message = commonUserVO.getUserName()+"，您好！在线监督平台提示:会议名为:"+conferenceArrange.getArrangeName()+"的会议，会议时间为"+conferenceArrange.getStartTime()+"——"+conferenceArrange.getEndTime()+"，报名时间截止"+conferenceArrange.getLastFullTime();
                    String mobileNo = commonUserVO.getPhonenumber();
                    if(StringUtils.isNotEmpty(mobileNo)){
                        SmsUtil.sendMessage(message, mobileNo);
                    }
                }
            }
        };
    }

    private TimerTask createWebsiteTips(List<CommonUserVO> list, int num) {
        return new TimerTask()
        {
            @Override
            public void run()
            {
                for (CommonUserVO commonUserVO : list) {
                    WorkHandle workHandle = new WorkHandle();
                    workHandle.setUserId(commonUserVO.getUserId());
                    workHandle.setType(CommonConstants.WORK);
                    workHandleService.insertOrUpdateWorkHandle(workHandle, num); //工作项+1
                }
            }
        };
    }

}
